package com.industry.framework.core.context.register;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.industry.framework.core.context.CoreApplicationContext;
import com.industry.framework.core.generic.GenericController;
import com.industry.framework.core.generic.GenericService;
import com.industry.framework.core.generic.chooser.CoreGenericChooser;
import com.industry.framework.core.generic.record.RecordScanner;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author nbZhuozh
 * @Date 2025-03-22  14:26
 **/
@Component
public class CoreRegisterContextInit extends AbstractCoreRegisterContext {


    private static final Logger logger = LoggerFactory.getLogger(CoreRegisterContextInit.class);


    @Override
    public void initCoreControllerClass(ApplicationContext applicationContext){
        //获取对应GenericController 的实现类
        CoreApplicationContext.GENERIC_CONTROLLER_MAP = applicationContext.getBeansOfType(GenericController.class);

        Map<String, GenericController> beansOfType = applicationContext.getBeansOfType(GenericController.class);
        if(!beansOfType.isEmpty()){
            beansOfType.forEach((k,v)->{
                String s = toUpperCaseFirstWord(k);
                CoreApplicationContext.GENERIC_CONTROLLER_MAP.put(s,v);
                logger.info("【idoo - GenericController】 init name :{}",s);
            });
        }
    }

    @Override
    public void initCoreServiceClass(ApplicationContext applicationContext){
        //获取对应GenericService 的实现类
        Map<String, GenericService> beansOfType = applicationContext.getBeansOfType(GenericService.class);
        if(!beansOfType.isEmpty()){
            beansOfType.forEach((k,v)->{
                String s = toUpperCaseFirstWord(k);
                CoreApplicationContext.GENERIC_SERVICE_MAP.put(s,v);
                logger.info("【idoo - GenericService】 init name :{}",s);
            });
        }
    }

    @Override
    public void initMyBatisBaseMapper(ApplicationContext applicationContext){
        Map<String, BaseMapper> beansOfType = applicationContext.getBeansOfType(BaseMapper.class);
        if(!beansOfType.isEmpty()){
            beansOfType.forEach((k,v)->{
                String s = toUpperCaseFirstWord(k);
                CoreApplicationContext.GENERIC_BASE_MAPPER.put(s,v);
                logger.info("【idoo - BaseMapper】 init name :{}",s);
            });
        }
    }

    @Override
    public void initCoreChooser(ApplicationContext applicationContext) {
        Map<String, CoreGenericChooser> beansOfType = applicationContext.getBeansOfType(CoreGenericChooser.class);
        if(!beansOfType.isEmpty()){
            beansOfType.forEach((k,v)->{
                String s = toUpperCaseFirstWord(k);
                CoreApplicationContext.GENERIC_BASE_CHOOSER.put(s,v);
                logger.info("【idoo - CoreFenericChooser】 init name :{}",s);
            });
        }
    }

    @Override
    public void initRecordBizHis() {
        RecordScanner recordScanner = CoreApplicationContext.getApplicationContext().getBean(RecordScanner.class);
        recordScanner.register();
    }


    private String toUpperCaseFirstWord(String str){
        if(str == null){
            return null;
        }
        return StringUtils.capitalize(str);
    }
}
